﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;

namespace BluePrintMaster
{
    class ExcelDataManager
    {
        private static ExcelDataManager mInstance;
        public static ExcelDataManager GetInstance()
        {
            if (mInstance == null)
            {
                mInstance = new ExcelDataManager();
                mInstance.LoadData();
            }
            return mInstance;
        }

        private string oldExcelPath = "";
        private string newExcelPath = "";
        public string OldExcelPath
        {
            get
            {
                return oldExcelPath;
            }
            set
            {
                oldExcelPath = value;
                myForm.label1.Text = oldExcelPath;
            }
        }
        public string NewExcelPath
        {
            get
            {
                return newExcelPath;
            }
            set
            {
                newExcelPath = value;
                myForm.label2.Text = newExcelPath;
            }
        }
        private Form1 myForm;

        public void SetForm(Form1 form)
        {
            myForm = form;
            myForm.label1.Text = oldExcelPath;
            myForm.label2.Text = newExcelPath;
        }
        public DataTable ReadOldExcel(int startColumn, int endColumn, int startRow, int endRow)
        {
            DataTable readData = new DataTable();
            FileInfo excel = new FileInfo(oldExcelPath);
            if (excel.Exists)
            {
                using (OfficeOpenXml.ExcelPackage package = new OfficeOpenXml.ExcelPackage(excel))
                {
                    // add a new worksheet to the empty workbook  
                    OfficeOpenXml.ExcelWorksheet worksheet = package.Workbook.Worksheets["2"];
                    for (int column = startColumn; column <= endColumn; column++)
                    {
                        DataColumn dataColumn = new DataColumn();
                        readData.Columns.Add(dataColumn);
                        for (int row = startRow; row <= endRow; row++)
                        {
                            if (row - startRow >= readData.Rows.Count)
                            {
                                DataRow dataRow = readData.NewRow();
                                readData.Rows.Add(dataRow);
                            }
                            readData.Rows[row - startRow][column - startColumn] = worksheet.Cells[row, column].Text;
                        }
                    }
                }
            }
            return readData;
        }

        public void LoadData()
        {
            string filePath = Path.Combine(System.Environment.CurrentDirectory, "local");
            FileInfo file = new FileInfo(filePath);
            if (file.Exists)
            {
                FileStream stream = new FileStream(filePath, FileMode.Open);
                StreamReader reader = new StreamReader(stream);
                oldExcelPath = reader.ReadLine();
                newExcelPath = reader.ReadLine();
                reader.Close();
                stream.Close();
            }
        }
        public void SaveData()
        {
            string filePath = Path.Combine(System.Environment.CurrentDirectory, "local");
            FileStream stream = new FileStream(filePath, FileMode.Create);
            StreamWriter reader = new StreamWriter(stream);
            reader.WriteLine(oldExcelPath);
            reader.WriteLine(newExcelPath);
            reader.Close();
            stream.Close();
        }
    }
}
